home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 May / EnigmA AMIGA RUN 18 (1997)(G.R. Edizioni)(IT)[!][issue 1997-05][EAR-CD II].iso / softwareupdate / system / amigados / advancedroutines / example5.c < prev    next >
C/C++ Source or Header  |  1996-10-10  |  7KB  |  233 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE)           Amiga C Club (ACC) */
  4. /* --------------------------           ------------------ */
  5. /*                                                         */
  6. /* Manual:  AmigaDOS                    Amiga C Club       */
  7. /* Chapter: Advanced Routines           Tulevagen 22       */
  8. /* File:    Example5.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    93-03-17                                       */
  11. /* Version: 1.2                                            */
  12. /*                                                         */
  13. /*   Copyright 1993, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* This example demonstrates how to use the Info() function  */
  21. /* to get some information about a disk. We will, among many */
  22. /* things, check if the disk is write protected or not, what */
  23. /* type of disk it is etc... In this example we examine the  */
  24. /* disk in "df0:".                                           */
  25. /*                                                           */
  26. /* This example can be used with all versions of the dos     */
  27. /* library.                                                  */
  28.  
  29.  
  30.  
  31. /* Include the dos library definitions: */
  32. #include <dos/dos.h>
  33.  
  34. /* Include memory definitions: (MEMF_ANY...) */
  35. #include <exec/memory.h>
  36.  
  37. /* Now we include the necessary function prototype files:         */
  38. #include <clib/dos_protos.h>       /* General dos functions...    */
  39. #include <clib/exec_protos.h>      /* System functions...         */
  40. #include <stdio.h>                 /* Std functions [printf()...] */
  41. #include <stdlib.h>                /* Std functions [exit()...]   */
  42.  
  43.  
  44.  
  45. /* The disk we want to examine: */
  46. #define DISK_STR "df0:"
  47.  
  48.  
  49.  
  50. /* Set name and version number: */
  51. UBYTE *version = "$VER: AmigaDOS/Advanced Routines/Example5 1.2";
  52.  
  53.  
  54.  
  55. /* Declared our own functions: */
  56. int main( int argc, char *argv[] );
  57.  
  58.  
  59.  
  60. /* Now the "real" program code starts: */
  61. int main( int argc, char *argv[] )
  62. {
  63.   /* A "BCPL" pointer to our lock: */
  64.   BPTR my_lock;
  65.  
  66.   /* A pointer to our info data block: */
  67.   struct InfoData *my_info_data;
  68.  
  69.   /* AmigaDOS boolean check variable: */
  70.   LONG ok;
  71.  
  72.  
  73.  
  74.   /* Allocate enough memory to store an InfoData structure in. Since */
  75.   /* Info() wants the memoryblock to be "longword-aligned" we have   */
  76.   /* to allocate it ourself. (A longword-aligned data block starts   */
  77.   /* on a complete 32-bit, longword, address). If we simply declare  */
  78.   /* the structure in the C code it might happen that it starts in   */
  79.   /* the middle of a longword address, and this could crash the      */
  80.   /* system!                                                         */
  81.   my_info_data = (struct InfoData *)
  82.     AllocMem( sizeof( struct InfoData ), MEMF_ANY );
  83.  
  84.   /* Have we successfully allocated the memory? */
  85.   if( !my_info_data )
  86.   {
  87.     /* Not enough memory! Inform the user and quit: */
  88.     printf( "Could not allocate enough memory!\n" );
  89.  
  90.     /* Exit with an error code: */
  91.     exit( 20 );
  92.   }
  93.  
  94.    
  95.   
  96.   /* Lock the disk, or any file on that disk, we want to examine: */
  97.   /* (Since we only want to examine the disk, read some stuff, we */
  98.   /* only have to put a shared lock on it.)                       */
  99.   my_lock = Lock( DISK_STR, SHARED_LOCK );
  100.  
  101.   /* Have we successfully locked the disk? */
  102.   if( !my_lock )
  103.   {
  104.     /* Could not lock the disk! */
  105.  
  106.     /* Inform the user: */
  107.     printf( "Could not lock the disk \"%s\"!\n", DISK_STR );
  108.  
  109.     /* Free the previously allocated memory: */
  110.     FreeMem( my_info_data, sizeof( struct InfoData ) );
  111.  
  112.     /* Exit with an error code: */
  113.     exit( 21 );
  114.   }
  115.  
  116.  
  117.   /* Once we have allocated some memory for the info data structure   */
  118.   /* and have locked the disk we want to examine, we may call Info(): */
  119.   ok = Info( my_lock, my_info_data );
  120.  
  121.   /* Have we successfully collected the data? */
  122.   if( !ok )
  123.   {
  124.     /* Problems, we could not examine the disk! */
  125.     
  126.     /* Inform the user: */
  127.     printf( "Could not examine the disk \"%s\"!\n", DISK_STR );
  128.  
  129.     /* Unlock the disk: */
  130.     UnLock( my_lock );
  131.  
  132.     /* Free the previously allocated memory: */
  133.     FreeMem( (APTR) my_info_data, sizeof( struct InfoData ) );
  134.  
  135.     /* Exit with an error code: */
  136.     exit( 22 );
  137.   }
  138.  
  139.   
  140.   
  141.   /* We can now examine the disk: (Here is a small */
  142.   /* demonstration on what you can do.)            */
  143.   printf( "\n\nHere is some information about the disk in drive %s\n\n",
  144.     DISK_STR );
  145.  
  146.   /* The unit number: */
  147.   printf( "Disk unit number: %d\n", my_info_data->id_UnitNumber );
  148.  
  149.  
  150.  
  151.   /* Disk Status: */
  152.  
  153.   /* Is the disk write protected? */
  154.   if( my_info_data->id_DiskState == ID_WRITE_PROTECTED )
  155.     printf( "The disk is Write Protected!\n" );
  156.  
  157.   /* Is the disk being validated? */
  158.   if( my_info_data->id_DiskState == ID_VALIDATING )
  159.     printf( "The disk is being validated!\n" );
  160.  
  161.   /* Is the disk validated but not write protected? */
  162.   if( my_info_data->id_DiskState == ID_VALIDATED )
  163.     printf( "The disk is Not (Write) Protected!\n" );
  164.  
  165.  
  166.  
  167.   /* Disk Types: */
  168.  
  169.   switch( my_info_data->id_DiskType )
  170.   {
  171.     case ID_NO_DISK_PRESENT:
  172.       printf( "No disk present\n" );
  173.       break;
  174.     case ID_UNREADABLE_DISK:
  175.       printf( "Unreadable disk\n" );
  176.       break;
  177.     case ID_DOS_DISK:
  178.       printf( "DOS number 0 (Nomal) disk\n" );
  179.       break;
  180.     case ID_FFS_DISK:
  181.       printf( "DOS number 1 (Fast FileSystem) disk\n" );
  182.       break;
  183.     case ID_INTER_DOS_DISK:
  184.       printf( "DOS number 3 (Int. Normal) disk\n" );
  185.       break;
  186.     case ID_INTER_FFS_DISK:
  187.       printf( "DOS number 4 (Int. Fast FileSystem) disk\n" );
  188.       break;
  189.     case ID_NOT_REALLY_DOS:
  190.       printf( "Not a dos disk\n" );
  191.       break;
  192.     case ID_KICKSTART_DISK:
  193.       printf( "kickstart disk\n" );
  194.       break;
  195.     case ID_MSDOS_DISK:
  196.       printf( "MS DOS disk\n" );
  197.       break;
  198.   }
  199.  
  200.  
  201.  
  202.   /* If the disk is in use or not: */
  203.   if( my_info_data->id_InUse )
  204.     printf( "The disk is in use.\n" );
  205.   else
  206.     printf( "The disk is not in use.\n" );
  207.  
  208.  
  209.   /* Print some disk size data: */
  210.   printf( "Number of blocks on disk:  %5d\n",
  211.     my_info_data->id_NumBlocks );
  212.  
  213.   printf( "Number of blocks used:     %5d\n",
  214.     my_info_data->id_NumBlocksUsed );
  215.  
  216.   printf( "Number of bytes per block: %5d\n\n",
  217.     my_info_data->id_BytesPerBlock );
  218.  
  219.  
  220.  
  221.  
  222.   /* Unlock the disk: */
  223.   UnLock( my_lock );
  224.  
  225.   /* Free the previously allocated memory: */
  226.   FreeMem( my_info_data, sizeof( struct InfoData ) );
  227.  
  228.   /* The End (0 = success): */
  229.   exit( 0 );
  230. }
  231.  
  232.  
  233.